package com.example.expresscabinet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.expresscabinet.dao.cabinetMapper;
import com.example.expresscabinet.dao.sendexpressMapper;
import com.example.expresscabinet.model.cabinet;
import com.example.expresscabinet.model.sendexpress;
import com.example.expresscabinet.service.ICabinetService;
import com.example.expresscabinet.service.IPostageService;
import com.example.expresscabinet.service.ISendexpressService;
import com.example.expresscabinet.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class sendexpressServiceImpl extends ServiceImpl<sendexpressMapper, sendexpress> implements ISendexpressService {

    @Resource
    sendexpressMapper sendexpressMapper;

    @Autowired
    IUserService userService;

    @Autowired
    ICabinetService cabinetService;

    @Autowired
    IPostageService postageService;


    //添加sendexpress
    @Override
    public sendexpress addSendexpress( String tel,int num,String fromWhere,String fromWhereDetail,String toWhere,String toWhereDetail,String company){
        sendexpress se=new sendexpress();
        se.setCode((int)((Math.random()*9+1)*100000)+"");
        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(nousedate);
        System.out.println(nousedate);
        se.setThetime(nousedate);
        se.setFromwhere(fromWhere+fromWhereDetail);
        se.setTowhere(toWhere+toWhereDetail);
        se.setNum(num);
        se.setTel(tel);
        se.setStates("未取走");
        se.setExpresstel(userService.queryFixExpressman(company));
        se.setCost(postageService.queryPostage(fromWhere,toWhere,company));
        sendexpressMapper.insert(se);
        cabinetService.takeOccupyCabinet(num);
        return se;
    }


    //更新状态为已取走
    @Override
    public void takeSendexpress(String expresstel,String code){
        QueryWrapper<sendexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("expresstel",expresstel).eq("code",code);
        sendexpress tem=sendexpressMapper.selectOne(wrapper);
        int num=tem.getNum();
        cabinetService.takeEmptyCabinet(num);
        sendexpressMapper.updateStates(expresstel,code);

    }

    @Override
    public Page<sendexpress> querynotake(String tel,int pageNum,int pageSize){
        QueryWrapper<sendexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel).eq("states","未取走");
        Page<sendexpress> res=sendexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }

    @Override
    public Page<sendexpress> querytake(String tel,int pageNum,int pageSize){
        QueryWrapper<sendexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel).eq("states","已取走");
        Page<sendexpress> res=sendexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }


    @Override
    public Page<sendexpress> querynotakebyexpressman(String expresstel,int pageNum,int pageSize){
        QueryWrapper<sendexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("expresstel",expresstel).eq("states","未取走");
        Page<sendexpress> res=sendexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }

    @Override
    public Page<sendexpress> querytakebyexpressman(String expresstel,int pageNum,int pageSize){
        QueryWrapper<sendexpress> wrapper=new QueryWrapper<>();
        wrapper.eq("expresstel",expresstel).eq("states","已取走");
        Page<sendexpress> res=sendexpressMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return res;
    }

}
